package com.hotel.service.Impl;

import com.hotel.constant.CommonConstant;
import com.hotel.entity.Permission;
import com.hotel.entity.User;
import com.hotel.mapper.PermissionMapper;
import com.hotel.mapper.UserMapper;
import com.hotel.mapper.UserRoleMapper;
import com.hotel.service.UserService;
import com.hotel.utils.CommonUtil;
import com.hotel.utils.HotelUtil;
import com.hotel.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 夏智力
 * @FileName UserServiceImpl
 * @date 2021/6/21 22:32
 * @Software: IntelliJ IDEA
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PermissionMapper PermissionMapper;
    @Autowired
    private UserRoleMapper UserRoleMapper;


    @Override
    public User selectById(int id) {
        return userMapper.selectById(id);
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public Map<String, Object> register(User user) {
        Map<String, Object> map = new HashMap<>();
        User u = userMapper.selectUserByName(user.getUsername());
        if(u != null) {
            map.put("usernameMsg", "该账号已存在");
        } else {
            user.setSalt(HotelUtil.generateUUID().substring(0,8));
            user.setPassword(HotelUtil.md5(user.getPassword() + user.getSalt()));
            user.setReleaseTime(new Date());
            userMapper.insertUser(user);
            map.put("success", "注册成功!");
        }
        return map;
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public int deleteUser(int id) {
        return userMapper.deleteUser(id);
    }

    @Override
    public User selectUserByName(String username) {
        return userMapper.selectUserByName(username);
    }

    /**
     * 以下是吴泽胜定义
     * @param username 用户名
     * @return
     */
    @Override
    public Set<String> selectUserRolesSet(String username) {
        // 查询用户拥有的角色集合
        List<String> roles = UserRoleMapper.selectRoleByUserName(username);
        log.info("-------通过数据库读取用户拥有的角色Rules------username： " + username + ",Roles size: " + (roles == null ? 0 : roles.size()));
        return new HashSet<>(roles);
    }

    @Override
    public Set<String> selectUserPermissionsSet(String username) {
        Set<String> permissionSet = new HashSet<>();
        List<Permission> permissionList = PermissionMapper.selectByUser(username);
        for (Permission po : permissionList) {
            if (CommonUtil.isNotEmpty(po.getPerms())) {
                permissionSet.add(po.getPerms());
            }
        }
        log.info("-------通过数据库读取用户拥有的权限Perms------username： " + username + ",Perms size: " + (permissionSet == null ? 0 : permissionSet.size()));
        return permissionSet;
    }

    @Override
    public Result checkUserIsEffective(User User) {
        Result<?> result = new Result<Object>();
        //情况1：根据用户信息查询，该用户不存在
        if (User == null) {
            result.error500("该用户不存在，请注册");
            return result;
        }
        //情况2：根据用户信息查询，该用户已注销
        if (CommonConstant.DEL_FLAG_1.toString().equals(User.getIs_deleted())) {
            result.error500("该用户已注销");
            return result;
        }
        //情况3：根据用户信息查询，该用户已被删除
        if (CommonConstant.USER_FREEZE.equals(User.getIs_deleted())) {
            result.error500("该用户已被删除");
            return result;
        }
        return result;
    }
}
